package io.inputstream;

import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

public class RandomInputStream extends InputStream {

	private Random generator = new Random();
	private boolean closed = false;

	@Override
	public int read() throws IOException {
		checkOpen();
		int result = generator.nextInt() % 256;
		if (result < 0) {
			result = -result;
		}
		return result;
	}

	@Override
	public int read(byte[] b, int off, int len) throws IOException {
		checkOpen();
		generator.nextBytes(b);
		return b.length;
	}

	@Override
	public int read(byte[] b) throws IOException {
		checkOpen();
		generator.nextBytes(b);
		return b.length;
	}
	
	@Override
	public long skip(long byteToSkip) throws IOException {
		checkOpen();
		return byteToSkip;
	}

	public void close() {
		this.closed = true;
	}

	private void checkOpen() throws IOException {
		if (closed)
			throw new IOException("Input stream closed");
	}

	public int available() {
		// Limited only by available memory and the size of an array.
		return Integer.MAX_VALUE;
	}

}
